import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _createSuper from "@babel/runtime/helpers/esm/createSuper";
import _applyDecoratedDescriptor from "@babel/runtime/helpers/esm/applyDecoratedDescriptor";
import _isUndefined from "lodash/isUndefined";
import _isFunction from "lodash/isFunction";
import _isEmpty from "lodash/isEmpty";

var _dec, _dec2, _dec3, _dec4, _dec5, _dec6, _dec7, _dec8, _dec9, _class, _class2;

import React from 'react';
import { Modal, Table, notification, Form, Button, Input } from 'antd';
import { Bind } from 'lodash-decorators';
import intl from 'utils/intl';
import { createPagination } from 'utils/utils';
import { SEARCH_COL_CLASSNAME } from 'utils/constants';
var FormItem = Form.Item;
/**
 * GroupModal-选择新的用户组 弹框
 * @reactProps {Boolean} visible 模态框是否显示
 * @reactProps {Boolean} dataSource 用户组的数据源
 * @reactProps {Function(selectedRows:Object[]):Promise|*} onSave 确认按钮的回调,接收选中的用户组,返回一个Promise对象或者任意值
 * @reactProps {Function} onCancel 取消按钮的回调
 */

var GroupModal = (_dec = Form.create({
  fieldNameProp: null
}), _dec2 = Bind(), _dec3 = Bind(), _dec4 = Bind(), _dec5 = Bind(), _dec6 = Bind(), _dec7 = Bind(), _dec8 = Bind(), _dec9 = Bind(), _dec(_class = (_class2 = /*#__PURE__*/function (_React$PureComponent) {
  _inherits(GroupModal, _React$PureComponent);

  var _super = _createSuper(GroupModal);

  function GroupModal(props) {
    var _this;

    _classCallCheck(this, GroupModal);

    _this = _super.call(this, props);
    _this.state = {
      selectedRowKeys: [],
      selectedRows: [],
      dataSource: [],
      pagination: false
    };
    return _this;
  }

  _createClass(GroupModal, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      this.fetchGroupList();
    }
  }, {
    key: "handleSearchBtnClick",
    value: function handleSearchBtnClick() {
      this.fetchGroupList();
    }
  }, {
    key: "fetchGroupList",
    value: function fetchGroupList() {
      var _this2 = this;

      var pagination = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {
        page: 0,
        size: 10
      };
      var _this$props = this.props,
          fetchGroups = _this$props.fetchGroups,
          form = _this$props.form,
          _this$props$excludeId = _this$props.excludeIds,
          excludeIds = _this$props$excludeId === void 0 ? [] : _this$props$excludeId,
          _this$props$userId = _this$props.userId,
          userId = _this$props$userId === void 0 ? [] : _this$props$userId,
          tenantId = _this$props.tenantId;

      var queryParams = _objectSpread(_objectSpread({}, pagination), {}, {
        excludeIds: excludeIds,
        userId: userId
      });

      var groupName = form.getFieldValue('groupName');
      var groupCode = form.getFieldValue('groupCode');

      if (!_isUndefined(groupCode)) {
        queryParams.groupCode = groupCode;
      }

      if (!_isUndefined(groupName)) {
        queryParams.groupName = groupName;
      }

      if (!_isUndefined(tenantId)) {
        queryParams.tenantId = tenantId;
      }

      this.setState({
        fetchGroupsLoading: true
      });
      fetchGroups(queryParams).then(function (res) {
        if (res) {
          _this2.setState({
            dataSource: res.content,
            pagination: createPagination(res),
            selectedRowKeys: [],
            selectedRows: []
          });
        }
      }).finally(function () {
        _this2.setState({
          fetchGroupsLoading: false
        });
      });
    }
  }, {
    key: "handleRowSelectionChange",
    value: function handleRowSelectionChange(_, selectedRows) {
      this.setState({
        selectedRowKeys: selectedRows.map(function (r) {
          return r.userGroupId;
        }),
        selectedRows: selectedRows
      });
    }
  }, {
    key: "handleTableChange",
    value: function handleTableChange() {
      var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
          _ref$current = _ref.current,
          current = _ref$current === void 0 ? 1 : _ref$current,
          _ref$pageSize = _ref.pageSize,
          pageSize = _ref$pageSize === void 0 ? 10 : _ref$pageSize;

      this.fetchGroupList({
        page: current - 1,
        size: pageSize
      });
    }
    /**
     * 保存
     */

  }, {
    key: "handleSaveBtnClick",
    value: function handleSaveBtnClick() {
      var selectedRows = this.state.selectedRows;
      this.save(selectedRows);
    }
    /**
     *
     * @param {Object[]} selectedRows
     */

  }, {
    key: "save",
    value: function save(selectedRows) {
      var onSave = this.props.onSave;

      if (_isEmpty(selectedRows)) {
        notification.warning({
          message: intl.get('ciam.subAccount.view.message.chooseNewGroupFirst').d('请先选择新增的用户组')
        });
      } else if (_isFunction(onSave)) {
        onSave(selectedRows);
        this.setState({
          selectedRowKeys: [],
          selectedRows: []
        });
      }
    }
    /**
     * 取消保存
     */

  }, {
    key: "handleCancelBtnClick",
    value: function handleCancelBtnClick() {
      var onCancel = this.props.onCancel;

      if (_isFunction(onCancel)) {
        this.setState({
          selectedRowKeys: [],
          selectedRows: []
        });
        onCancel();
      }
    }
  }, {
    key: "handleResetBtnClick",
    value: function handleResetBtnClick() {
      var resetFields = this.props.form.resetFields;
      resetFields();
    }
  }, {
    key: "render",
    value: function render() {
      var _this$props2 = this.props,
          visible = _this$props2.visible,
          getFieldDecorator = _this$props2.form.getFieldDecorator;
      var _this$state = this.state,
          selectedRowKeys = _this$state.selectedRowKeys,
          _this$state$dataSourc = _this$state.dataSource,
          dataSource = _this$state$dataSourc === void 0 ? [] : _this$state$dataSourc,
          _this$state$paginatio = _this$state.pagination,
          pagination = _this$state$paginatio === void 0 ? false : _this$state$paginatio,
          fetchGroupsLoading = _this$state.fetchGroupsLoading;
      var columns = [{
        title: intl.get('ciam.subAccount.model.group.groupCode').d('用户组编码'),
        dataIndex: 'groupCode',
        width: 200
      }, {
        title: intl.get('ciam.subAccount.model.group.groupName').d('用户组名称'),
        dataIndex: 'groupName',
        width: 200
      }];
      return /*#__PURE__*/React.createElement(Modal, {
        visible: visible,
        onOk: this.handleSaveBtnClick,
        onCancel: this.handleCancelBtnClick,
        width: 720,
        title: intl.get('ciam.subAccount.view.message.title.groupModal').d('选择用户组')
      }, /*#__PURE__*/React.createElement(Form, {
        layout: "inline"
      }, /*#__PURE__*/React.createElement(FormItem, {
        label: intl.get('ciam.subAccount.model.group.groupCode').d('用户组编码')
      }, getFieldDecorator('groupCode')( /*#__PURE__*/React.createElement(Input, null))), /*#__PURE__*/React.createElement(FormItem, {
        label: intl.get('ciam.subAccount.model.group.groupName').d('用户组名称')
      }, getFieldDecorator('groupName')( /*#__PURE__*/React.createElement(Input, null))), /*#__PURE__*/React.createElement(FormItem, {
        className: SEARCH_COL_CLASSNAME
      }, /*#__PURE__*/React.createElement(Button, {
        onClick: this.handleResetBtnClick
      }, intl.get('cpaas.common.button.reset').d('重置')), /*#__PURE__*/React.createElement(Button, {
        htmlType: "submit",
        type: "primary",
        onClick: this.handleSearchBtnClick
      }, intl.get('cpaas.common.button.search').d('查询')))), /*#__PURE__*/React.createElement(Table, {
        bordered: true,
        rowKey: "userGroupId",
        loading: fetchGroupsLoading,
        dataSource: dataSource,
        pagination: pagination,
        columns: columns,
        style: {
          marginTop: 14
        } // scroll={{ x: tableScrollWidth(columns), y: 400 }}
        ,
        rowSelection: {
          selectedRowKeys: selectedRowKeys,
          onChange: this.handleRowSelectionChange
        },
        onChange: this.handleTableChange
      }));
    }
  }]);

  return GroupModal;
}(React.PureComponent), (_applyDecoratedDescriptor(_class2.prototype, "handleSearchBtnClick", [_dec2], Object.getOwnPropertyDescriptor(_class2.prototype, "handleSearchBtnClick"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "fetchGroupList", [_dec3], Object.getOwnPropertyDescriptor(_class2.prototype, "fetchGroupList"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "handleRowSelectionChange", [_dec4], Object.getOwnPropertyDescriptor(_class2.prototype, "handleRowSelectionChange"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "handleTableChange", [_dec5], Object.getOwnPropertyDescriptor(_class2.prototype, "handleTableChange"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "handleSaveBtnClick", [_dec6], Object.getOwnPropertyDescriptor(_class2.prototype, "handleSaveBtnClick"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "save", [_dec7], Object.getOwnPropertyDescriptor(_class2.prototype, "save"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "handleCancelBtnClick", [_dec8], Object.getOwnPropertyDescriptor(_class2.prototype, "handleCancelBtnClick"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "handleResetBtnClick", [_dec9], Object.getOwnPropertyDescriptor(_class2.prototype, "handleResetBtnClick"), _class2.prototype)), _class2)) || _class);
export { GroupModal as default };